package com.thd.springboot.project.camundamanager.controller;

import com.github.pagehelper.PageInfo;
import com.thd.springboot.framework.MyException;
import com.thd.springboot.framework.constants.CommonConstants;
import com.thd.springboot.framework.model.Message;
import com.thd.springboot.framework.web.controller.BasicController;
import com.thd.springboot.project.camundamanager.dto.*;
import com.thd.springboot.project.camundamanager.entity.*;
import com.thd.springboot.project.camundamanager.service.ProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/camundaManager")
@Api("流程管理")
public class CamundaController extends BasicController {
    @Autowired
    private ProcessService processService;

    private Logger logger = LoggerFactory.getLogger(CamundaController.class);
    @ResponseBody
    @RequestMapping("/test")
    public Message<String> test(){
        logger.info("test");
        return Message.success(CommonConstants.STATUS_SUCCESS);
    }

    @ApiOperation(value="流程部署 - 部署新流程")
    @ResponseBody
    @PostMapping("/deployProcDef")
    public Message<ProcDeployEntity> deployProcDef(@RequestParam("file") MultipartFile file) throws Exception{
        logger.info("deployProcDef...");

        // 文件验证
        if(!file.getOriginalFilename().substring(file.getOriginalFilename().length() -5 ,file.getOriginalFilename().length() ).equalsIgnoreCase(".bpmn")){
            throw new MyException("500","The file suffix must be '.bpmn'");
        }

        ProcDeployEntity entity = this.processService.deployProcDef(file.getInputStream(),file.getOriginalFilename());
        return Message.success(entity);
    }



    @ApiOperation(value="流程部署 - 根据ID删除")
    @RequestMapping("/deleteProcDefDeployById/{deploymentId}")
    @ResponseBody
    public Message deleteProcDefDeployById(@PathVariable String deploymentId) {
        logger.info("deleteProcDefDeployById...");
        this.processService.deleteProcDefDeployById(deploymentId);
        return Message.success();
    }


    @ApiOperation(value="流程定义 - 根据ID查询")
    @RequestMapping("/queryProcDefById/{procDefId}")
    @ResponseBody
    public Message<ProcDefEntity> queryProcDefById(@PathVariable String procDefId) {
        logger.info("queryProcDefById...");
        ProcDefEntity entity = this.processService.queryProcDefById(procDefId);
        return Message.success(entity);
    }


    @ApiOperation(value="流程定义 - 分页查询")
    @PostMapping("/queryProcessDefPage")
    @ResponseBody
    // url : http://127.0.0.1:2345/queryProcessDefPage
    public Message<PageInfo<ProcDefEntity>> queryProcessDefPage(@RequestBody ProcDefEntity condition) throws Exception{
        logger.info("queryProcessDef");
        PageInfo<ProcDefEntity> result = this.processService.queryProcessDefPage(condition);
        return Message.success(result);
    }

    @ApiOperation(value="流程定义 - 不分页查询")
    @PostMapping("/queryProcessDefList")
    @ResponseBody
    // url : http://127.0.0.1:2345/queryProcessDefList
    public Message<List<ProcDefEntity>> queryProcessDefList(@RequestBody ProcDefEntity condition) throws Exception{
        logger.info("queryProcessDef");
        List<ProcDefEntity> result = this.processService.queryProcessDefList(condition);
        return Message.success(result);
    }


    @ApiOperation(value="流程部署 - 根据ID删除")
    @RequestMapping("/deleteProcessDefById/{processDefId}")
    @ResponseBody
    public Message deleteProcessDefById(@PathVariable String processDefId) {
        logger.info("deleteProcessDefById...");
        this.processService.deleteProcessDefById(processDefId);
        return Message.success();
    }

    @ApiOperation(value="流程实例 - 开启")
    @PostMapping("/startProcess")
    @ResponseBody
    // url : http://127.0.0.1:2345/startProcess
    public Message<ProcessInstanceEntity> startProcess(@RequestBody StartProcessInstanceDto startProcessInstanceDto) throws Exception{
        logger.info("startProcess");
        ProcessInstanceEntity entity = this.processService.startProcess(startProcessInstanceDto);
        return Message.success(entity);
    }

    @ApiOperation(value="流程实例 - 挂起流程实例")
    @RequestMapping("/suspendsProcessInstance/{processInstanceId}")
    @ResponseBody
    public Message suspendsProcessInstance(@PathVariable String processInstanceId) {
        logger.info("suspendsProcessInstance...");
        this.processService.suspendsProcessInstance(processInstanceId);
        return Message.success();
    }

    @ApiOperation(value="流程实例 - 恢复挂起的流程实例")
    @RequestMapping("/activateProcessInstance/{processInstanceId}")
    @ResponseBody
    public Message activateProcessInstance(@PathVariable String processInstanceId) {
        logger.info("activateProcessInstance...");
        this.processService.activateProcessInstance(processInstanceId);
        return Message.success();
    }


    @ApiOperation(value="流程实例 - 取消流程实例")
    @RequestMapping("/cancelProcessInstance/{processInstanceId}")
    @ResponseBody
    public Message cancelProcessInstance(@PathVariable String processInstanceId) {
        logger.info("cancelProcessInstance...");
        this.processService.cancelProcessInstance(processInstanceId,"Cancel Process Instance");
        return Message.success();
    }



    @ApiOperation(value="流程实例 - 分页查询")
    @PostMapping(value="/queryProcessInstancePage")
    @ResponseBody
    public Message<PageInfo<ProcessInstanceEntity>> queryProcessInstancePage(@RequestBody ProcessInstanceEntity condition){
        logger.info("queryProcessInstancePage...");
        PageInfo<ProcessInstanceEntity> result = this.processService.queryProcessInstancePage(condition);
        return Message.success(result);
    };



    @ApiOperation(value="流程实例 - 根据ID查询流程实例详细信息-带有任务列表")
    @RequestMapping("/queryProcessInstanceDetailByInstanceId/{processInstanceId}")
    @ResponseBody
    public Message<ProcessInstanceEntity> queryProcessInstanceDetailByInstanceId(@PathVariable String processInstanceId) {
        logger.info("queryTaskByTaskId...");
        ProcessInstanceEntity processInstanceEntity = this.processService.queryProcessInstanceDetailByInstanceId(processInstanceId);
        return Message.success(processInstanceEntity);
    }


    @ApiOperation(value="任务查询 - 分页查询")
    @PostMapping(value="/queryTaskPage")
    @ResponseBody
    // url : http://127.0.0.1:2345/queryTaskPage
    public Message<PageInfo<ProcessInstanceTaskEntity>> queryTaskPage(@RequestBody ProcessInstanceTaskEntity condition) throws Exception{
        logger.info("queryTaskPage...");
        PageInfo<ProcessInstanceTaskEntity> result = this.processService.queryTaskPage(condition);
        return Message.success(result);
    }

    @ApiOperation(value="任务查询 - 不分页查询")
    @PostMapping(value="/queryTaskList")
    @ResponseBody
    // url : http://127.0.0.1:2345/queryTaskList
    public Message<List<ProcessInstanceTaskEntity>> queryTaskList(@RequestBody ProcessInstanceTaskEntity condition) throws Exception{
        logger.info("queryTaskList...");
        List<ProcessInstanceTaskEntity> result = this.processService.queryTaskList(condition);
        return Message.success(result);
    }

    @ApiOperation(value="任务 - 根据ID查询任务")
    @RequestMapping("/queryTaskByTaskId/{taskId}")
    @ResponseBody
    public Message<ProcessInstanceTaskEntity> queryTaskByTaskId(@PathVariable String taskId) {
        logger.info("queryTaskByTaskId...");
        ProcessInstanceTaskEntity taskEntity = this.processService.queryTaskByTaskId(taskId);
        return Message.success(taskEntity);
    }

    @ApiOperation(value="任务 - 完成任务")
    @PostMapping(value="/completeTask")
    @ResponseBody
    // url : http://127.0.0.1:2345/completeTask
    public Message completeTask(@RequestBody CompleteTaskDto completeTaskDto) throws Exception{
        logger.info("completeTask...");
        this.processService.completeTask(completeTaskDto);
        return Message.success();
    }

    @ApiOperation(value="任务 - 添加候选人/待办人")
    @PostMapping(value="/addUserToTask")
    @ResponseBody
    // url : http://127.0.0.1:2345/addUserToTask
    public Message addUserToTask(@RequestBody UserOfTaskDto userOfTaskDto) throws Exception{
        logger.info("addUserToTask...");
        this.processService.addUserToTask(userOfTaskDto);
        return Message.success();
    }

    @ApiOperation(value="任务 - 移除候选人/代办人")
    @PostMapping(value="/removeUserToTask")
    @ResponseBody
    // url : http://127.0.0.1:2345/removeUserToTask
    public Message removeUserToTask(@RequestBody UserOfTaskDto userOfTaskDto) throws Exception{
        logger.info("removeUserToTask...");
        this.processService.removeUserToTask(userOfTaskDto);
        return Message.success();
    }


    @ApiOperation(value="任务 - 任务委派")
    @PostMapping(value="/delegateTask")
    @ResponseBody
    // url : http://127.0.0.1:2345/delegateTask
    public Message delegateTask(@RequestBody DelegateTaskDto delegateTaskDto) throws Exception{
        logger.info("delegateTask...");
        this.processService.delegateTask(delegateTaskDto.getTaskId(),delegateTaskDto.getDelegateUserId());
        return Message.success();
    }


    @ApiOperation(value="任务 - 委派人交还任务")
    @GetMapping(value="/resolveTask/{taskId}")
    @ResponseBody
    // url : http://127.0.0.1:2345/resolveTask
    public Message resolveTask(@PathVariable String taskId) throws Exception{
        logger.info("resolveTask...");
        this.processService.resolveTask(taskId);
        return Message.success();
    }





    @ApiOperation(value="流程变量 - 根据taskId设置流程变量")
    @PostMapping(value="/addVariableToTask")
    @ResponseBody
    // url : http://127.0.0.1:2345/addVariableToTask
    public Message addVariableToTask(@RequestBody ProcessVariableDto processVariableDto) throws Exception{
        logger.info("addVariableToTask...");
        this.processService.addVariableToTask(processVariableDto.getId(),processVariableDto.getProcessVar());
        return Message.success();
    }


    @ApiOperation(value="流程变量 - 根据processInstanceId设置流程变量")
    @PostMapping(value="/addVariableToProcessInstance")
    @ResponseBody
    // url : http://127.0.0.1:2345/addVariableToProcessInstance
    public Message addVariableToProcessInstance(@RequestBody ProcessVariableDto processVariableDto) throws Exception{
        logger.info("addVariableToProcessInstance...");
        this.processService.addVariableToProcessInstance(processVariableDto.getId(),processVariableDto.getProcessVar());
        return Message.success();
    }


    @ApiOperation(value="流程变量 - 根据taskId查询")
    @RequestMapping("/queryVariableByTaskId/{taskId}")
    @ResponseBody
    public Message<ProcessInstanceTaskEntity> queryVariableByTaskId(@PathVariable String taskId) {
        logger.info("queryVariableByTaskId...");
        Map<String,Object> processVar = this.processService.queryVariableByTaskId(taskId);
        return Message.success(processVar);
    }

    @ApiOperation(value="流程变量 - 根据processInstanceId查询")
    @RequestMapping("/queryVariableByProcessInstanceId/{processInstanceId}")
    @ResponseBody
    public Message<ProcessInstanceTaskEntity> queryVariableByProcessInstanceId(@PathVariable String processInstanceId) {
        logger.info("queryVariableByProcessInstanceId...");
        Map<String,Object> processVar = this.processService.queryVariableByProcessInstanceId(processInstanceId);
        return Message.success(processVar);
    }

    @ApiOperation(value="历史任务 - 查询")
    @PostMapping(value="/queryTaskHisList")
    @ResponseBody
    // url : http://127.0.0.1:2345/queryTaskHisList
    public Message<List<ProcessInstanceTaskEntity>> queryTaskHisList(@RequestBody ProcessInstanceTaskHisEntity processInstanceTaskHisEntity) throws Exception{
        logger.info("queryTaskHisList...");
        List<ProcessInstanceTaskEntity> l = this.processService.queryTaskHisList(processInstanceTaskHisEntity);
        return Message.success(l);
    }

//    @ApiOperation(value="历史任务 - 根据ID查询")
//    @RequestMapping("/queryTaskHisByTaskId/{taskHisId}")
//    @ResponseBody
//    public Message<ProcessInstanceTaskHisEntity> queryTaskHisByTaskId(@PathVariable String taskHisId) {
//        logger.info("queryTaskHisByTaskId...");
//        ProcessInstanceTaskEntity entity = this.processService.queryTaskHisByTaskId(taskHisId);
//        return Message.success(entity);
//    }






}
